import { useAppStoreHook } from '@/store/modules/app';
import CryptoJS from 'crypto-js';
/**
 * Check if an element has a class
 * @param {HTMLElement} ele
 * @param {string} cls
 * @returns {boolean}
 */
export function hasClass(ele: HTMLElement, cls: string) {
  return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}

/**
 * Add class to element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function addClass(ele: HTMLElement, cls: string) {
  if (!hasClass(ele, cls)) ele.className += " " + cls;
}

/**
 * Remove class from element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function removeClass(ele: HTMLElement, cls: string) {
  if (hasClass(ele, cls)) {
    const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
    ele.className = ele.className.replace(reg, " ");
  }
}

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
  const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
  return isExternal;
}
//判断字符串是否为空
export function isEmpty(str:string) {
  if (typeof str == "undefined" || str == null || str == "") {
    return true;
  } else {
    return false;
  }
}

export function isInPermission(perStr:string){
  const { abpConfig } = useAppStoreHook();
  let perms = abpConfig.auth.grantedPolicies;
  return perms[perStr];
  // console.log('hasPerm',perms,hasPerm)
}


export function encrypt(data:any, key:string) {
  // 将不同类型的数据转换为字符串
  const dataString = JSON.stringify(data);
  // AES 加密
  const ciphertext = CryptoJS.AES.encrypt(dataString, key).toString();
  return ciphertext;
}

export function decrypt(ciphertext:string, key:string) {
  // AES 解密
  const bytes  = CryptoJS.AES.decrypt(ciphertext, key);
  const decryptedString = bytes.toString(CryptoJS.enc.Utf8);
  // 将 JSON 字符串转换回原始数据
  const decryptedData = JSON.parse(decryptedString);
  return decryptedData;
}
